﻿using UnityEngine;
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using Holoville.HOTween;

public partial class FriendScene : Scene
{	
	public GameObject requestFriendCell;
	public GameObject requestFriendNoneGroup;
	public ObjectPooler requestFriendObjectPooler;
	public Badge requestFriendBadge;
	public UIPanel requestFriendPanel;
	public UIScrollView requestFriendScrollView;
	public UIGrid requestFriendGrid;

	int requestFriendCount;

	Dictionary<int, FriendModel> requestFriendList = new Dictionary<int, FriendModel>();
	Dictionary<int, GameObject> requestFriendObjectList = new Dictionary<int, GameObject>();

	public void LoadRequestFriend(bool isScrollViewReposition)
	{
		NetworkManager.Instance.C2S_FRIEND_LIST( new int[]{ 1 }, (responseData) => {

			if (responseData.ecode != 0)
			{
				PopupUtility.OneButtonErrorPopup(responseData.ecode);
				return;
			}

			responseData.data = ExtractRequestFriendList(responseData.data);

			GameObject g = null;
			int idx = 0;

			ShowBadge(BadgeType.RequestFriend);

			RemoveAllRequestFriend();
			
			if (responseData.data.Count <= 0)
			{
				NGUITools.SetActive(requestFriendNoneGroup, true);
				return;
			}
			
			NGUITools.SetActive(requestFriendNoneGroup, false);
			
			responseData.data = responseData.data.OrderByDescending(eachObject=>eachObject.ReqDate).ToList();
			
			foreach (FriendModel fm in responseData.data)
			{
				g = requestFriendObjectPooler.GetPooledObject();
				g.transform.parent = requestFriendGrid.transform;
				g.transform.localScale = Vector3.one;
				g.transform.localRotation = Quaternion.identity;
				
				RequestFriendCell rfc = g.GetComponent<RequestFriendCell>();
				rfc.indexPath = idx;
				rfc.nameLabel.text = fm.name;
				rfc.levelLabel.text = string.Format("Lv {0}", fm.lv.ToString());
				rfc.lastAccessLabel.text = DatetimeToFomattedString(fm.lastAccess);
				
				switch(fm.type)
				{
				case 2:
					NGUITools.SetActive(rfc.acceptButton, true);
					NGUITools.SetActive(rfc.rejectButton, true);
					NGUITools.SetActive(rfc.cancelButton, false);
					break;
					
				case 3:
					NGUITools.SetActive(rfc.acceptButton, false);
					NGUITools.SetActive(rfc.rejectButton, false);
					NGUITools.SetActive(rfc.cancelButton, true);
					break;
				}
				
				requestFriendObjectList.Add (idx, g);
				requestFriendList.Add (idx, fm);

				NGUITools.SetActive(g, true);

				idx++;
			}
			
			requestFriendGrid.Reposition();

			if (isScrollViewReposition) requestFriendScrollView.ResetPosition();
		});
	}
	
	public void CancelRequestProcess(int selectedIndex)
	{
		NetworkManager.Instance.C2S_FRIEND_SEND_REQ_REMOVE(requestFriendList[selectedIndex].id, (responseData) => {

			if (responseData.ecode != 0)
			{
				PopupUtility.OneButtonErrorPopup(responseData.ecode);
				return;
			}
			
			LoadRequestFriend(false);
		});
	}
	
	public void AcceptRequestProcess(int selectedIndex, int type)
	{
		if (defaultFriendCount >= 30)
		{
			OpenOneButtonPopup(Localization.Get("uiCommonTextwarning"), Localization.Get("uiPopupFriendCaution3"));
			return;
		}

		NetworkManager.Instance.C2S_FRIEND_REQ_ACCEPT(requestFriendList[selectedIndex].id, type, (responseData) => {

			if (responseData.ecode != 0)
			{
				PopupUtility.OneButtonErrorPopup(responseData.ecode);
				return;
			}

			UserData.Instance.friendDayLimitCheckCount++;

			LoadRequestFriend(false);
			ReloadDefaultFriendCount();
		});
	}

	void RemoveAllRequestFriend()
	{
		requestFriendList.Clear();
		// requestFriendGrid.transform.DetachChildren();

		foreach (KeyValuePair<int, GameObject> pair in requestFriendObjectList)
		{
			NGUITools.SetActive(pair.Value, false);
		}
		requestFriendObjectList.Clear();
	}

	bool CanAddFriendByDayLimit()
	{
		DateTime _storedDateTime = UserData.Instance.friendDayLimitCheckTime;
		short _storedCount = UserData.Instance.friendDayLimitCheckCount;
		DateTime _nowDateTime = DateTime.Now;

		if (_storedDateTime.Year == _nowDateTime.Year && _storedDateTime.Month == _nowDateTime.Month && _storedDateTime.Day == _nowDateTime.Day)
		{
			if (_storedCount < 2)
				return true;
			else
				return false;
		}
		else
		{
			UserData.Instance.friendDayLimitCheckTime = _nowDateTime;
			UserData.Instance.friendDayLimitCheckCount = 0;
			return true;
		}
	}

	List<FriendModel> ExtractRequestFriendList(List<FriendModel> friendList)
	{
		List<FriendModel> _fl = new List<FriendModel>();
		foreach(FriendModel _fm in friendList)
		{
			if (_fm.type == 2 || _fm.type == 3)
				_fl.Add(_fm);
		}
		
		return _fl;
	}
}